//
//  ViewController.m
//  myhuibao
//
//  Created by lijun on 13-2-4.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "RestaurantViewController.h"
#import "RestaurantSearchViewController.h"
#import "ASIHTTPRequest.h"
//#import "ASIFormDataRequest.h"
#import "SBJson.h"
#import<QuartzCore/QuartzCore.h>

@interface RestaurantViewController ()

@end

@implementation RestaurantViewController
@synthesize mainView;
@synthesize edtCity;
@synthesize edtArea;
@synthesize edtKeyword;
@synthesize edtCuisine;
@synthesize edtDiscount;
@synthesize edtReiview;
@synthesize btnBack;
//@synthesize options = _options;
//@synthesize areas = _areas;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    edtKeyword.delegate = self;
    [self initDefault];
    [self getTables];
}

- (void)vewDidUnload
{
    [self setEdtCity:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"餐厅搜索";
    }
    return self;
}
- (void)dealloc {
    [edtCity release];
//    [_options release];
//    [_areas release];

    [mainView release];
    [edtArea release];
    [edtKeyword release];
    [edtCuisine release];
    [edtDiscount release];
    [edtReiview release];
    [btnBack release];
    [super dealloc];
}
//- (IBAction)showCityList:(id)sender {
//    edtCity.userInteractionEnabled=NO;
//    self.edtCity.text = @"test";
//    edtCity.userInteractionEnabled=YES;
//
//}
- (void)initDefault
{
    if(_showback==1)
        btnBack.hidden = NO;
    else
        btnBack.hidden = YES;
    mainView.layer.cornerRadius=5;
    mainView.layer.borderWidth=1;
    mainView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    
    edtCity.text = [NSString stringWithFormat:@"城市： %@",@"合肥"];
    cityId = 1;
    areaId = 0;
    cuisineId = 0;
    discountId = 0;
    reviewId = 0;
    //[5-6折]--5,[6-7折]--6,[7-8折]--7,[8-9折]--8,[9-10折]--9, --0
    arrayDiscount = [[NSArray arrayWithObjects:
                    [NSDictionary dictionaryWithObjectsAndKeys:@"不限",@"text", @"0",@"id",nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"9-10折",@"text", @"9",@"id",nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"8-9折",@"text", @"8",@"id",nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"7-8折",@"text", @"7",@"id",nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"6-7折",@"text", @"6",@"id",nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"5-6折",@"text", @"5",@"id",nil],
                 nil] retain];
    //[5-4]--5,[4-3]--4,[3-2]--3,[2-1]--2,[无评分]--1, 不限--0 
    arrayReivew = [[NSArray arrayWithObjects:
                      [NSDictionary dictionaryWithObjectsAndKeys:@"不限",@"text", @"0",@"id",nil],
                      [NSDictionary dictionaryWithObjectsAndKeys:@"5-4",@"text", @"5",@"id",nil],
                      [NSDictionary dictionaryWithObjectsAndKeys:@"4-3",@"text", @"4",@"id",nil],
                      [NSDictionary dictionaryWithObjectsAndKeys:@"3-2",@"text", @"3",@"id",nil],
                      [NSDictionary dictionaryWithObjectsAndKeys:@"2-1",@"text", @"2",@"id",nil],
                      [NSDictionary dictionaryWithObjectsAndKeys:@"无评分",@"text", @"1",@"id",nil],
                      nil] retain];
}
- (void) getTables{
    //异步请求
    NSURL *url = [NSURL URLWithString:url_city];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request setDelegate: self];
    [request startAsynchronous];
    
}
//下面是两个代理函数,当数据请求成功时会调用requestFinished，请求失败时（如网络问题或服务器内部错误）会调用requestFailed
- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *jsonStr = [request responseString];
    NSDictionary *dic = [jsonStr JSONValue];
    arrayCity = [dic valueForKey:@"data"];
    dic =[arrayCity objectAtIndex:0];
    cityId = [[dic valueForKey:@"cityID"] intValue];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?cityID=%d",url_area,cityId]] ;
    request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    jsonStr = [request responseString];
    dic = [jsonStr JSONValue];
    arrayArea = [dic valueForKey:@"data"];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"0",@"areaID",@"不限",@"areaName",nil];
    [arrayArea insertObject: dict atIndex:0];
    //    NSLog(@"%@",arrayArea);
    
    url = [NSURL URLWithString:url_cuisine] ;
    request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    jsonStr = [request responseString];
    dic = [jsonStr JSONValue];
    arrayCuisine = [dic valueForKey:@"data"];
//    NSLog(@"%@", arrayCuisine);

    dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"0",@"cuisineID",@"不限",@"cuisineName",nil];
    [arrayCuisine insertObject: dict atIndex:0];
    
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
}
- (IBAction)showCity:(id)sender {
    selectIndex = 1;
    LeveyPopListView *lplv = [[LeveyPopListView alloc] initWithTitle:@"请选择城市..." options:arrayCity];
    lplv.name = @"cityName";
    lplv.delegate = self;
    [lplv showInView:self.view animated:YES];
    [lplv release];

}

- (IBAction)showArea:(id)sender {
    selectIndex = 2;
    LeveyPopListView *lplv = [[LeveyPopListView alloc] initWithTitle:@"请选择商圈..." options:arrayArea];
    lplv.name = @"areaName";
    lplv.delegate = self;
    [lplv showInView:self.view animated:YES];
    [lplv release];
}

- (IBAction)showCuisine:(id)sender {
    selectIndex = 3;
    LeveyPopListView *lplv = [[LeveyPopListView alloc] initWithTitle:@"请选择菜系..." options:arrayCuisine];
    lplv.name = @"cuisineName";
    lplv.delegate = self;
    [lplv showInView:self.view animated:YES];
    [lplv release];
}

- (IBAction)showDiscount:(id)sender {
    selectIndex = 4;
    LeveyPopListView *lplv = [[LeveyPopListView alloc] initWithTitle:@"请选择折扣..." options:arrayDiscount];
    lplv.name = @"text";
    lplv.delegate = self;
    [lplv showInView:self.view animated:YES];
    [lplv release];
}

- (IBAction)showReivew:(id)sender {
    selectIndex = 5;
    LeveyPopListView *lplv = [[LeveyPopListView alloc] initWithTitle:@"请选择评分..." options:arrayReivew];
    lplv.name = @"text";
    lplv.delegate = self;
    [lplv showInView:self.view animated:YES];
    [lplv release];
}
- (void)viewDidUnload {
    [self setMainView:nil];
    [self setEdtArea:nil];
    [self setEdtKeyword:nil];
    [self setEdtCuisine:nil];
    [self setEdtDiscount:nil];
    [self setEdtReiview:nil];
//    [mainView release];
    mainView = nil;
    [self setBtnBack:nil];
    [super viewDidUnload];
}

#pragma mark - LeveyPopListView delegates
- (void)leveyPopListView:(LeveyPopListView *)popListView didSelectedIndex:(NSInteger)anIndex
{
    switch (selectIndex) {
        case 1:{
            edtCity.text = [NSString stringWithFormat:@"城市： %@",[[arrayCity objectAtIndex:anIndex] objectForKey:@"cityName"]];
            cityId = [[[arrayCity objectAtIndex:anIndex] objectForKey:@"cityID"] intValue];
            break;
        }
        case 2:{
            edtArea.text = [NSString stringWithFormat:@"商圈： %@",[[arrayArea objectAtIndex:anIndex] objectForKey:@"areaName"]];
            
            areaId = [[[arrayArea objectAtIndex:anIndex] objectForKey:@"areaID"] intValue];
            break;
        }
        case 3:{
            edtCuisine.text = [NSString stringWithFormat:@"菜系： %@",[[arrayCuisine objectAtIndex:anIndex] objectForKey:@"cuisineName"]];
            
            cuisineId = [[[arrayCuisine objectAtIndex:anIndex] objectForKey:@"areaID"] intValue];
            break;
        }
        case 4:{
            edtDiscount.text = [NSString stringWithFormat:@"折扣： %@",[[arrayDiscount objectAtIndex:anIndex] objectForKey:@"text"]];
            
            discountId = [[[arrayDiscount objectAtIndex:anIndex] objectForKey:@"id"] intValue];
            break;
        }
        case 5:{
            edtReiview.text = [NSString stringWithFormat:@"评分： %@",[[arrayReivew objectAtIndex:anIndex] objectForKey:@"text"]];
            
            reviewId = [[[arrayReivew objectAtIndex:anIndex] objectForKey:@"id"] intValue];
            break;
        }
        default:
            break;
    }

    
}
- (void)leveyPopListViewDidCancel
{
    //edtCity.text = @"You have cancelled";
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
//    if (theTextField == self.textField) {
        [theTextField resignFirstResponder];
//    }
    return YES;
}

- (IBAction)showSearch:(id)sender {
    RestaurantSearchViewController  *view = [[RestaurantSearchViewController alloc] initWithNibName:@"RestaurantSearchViewController" bundle:nil];
    view.cityId = cityId;
    view.areaId = areaId;
    view.cuisineId = cuisineId;
    view.discoutId = discountId;
    view.reviewId = reviewId;
    view.keyword = edtKeyword.text;
    view.hidesBottomBarWhenPushed=YES;
	[self.navigationController pushViewController:view animated:YES];
	[view release];
}
- (IBAction)doBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];

}
@end
